package com.lc.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lc.reggie.common.BaseContext;
import com.lc.reggie.common.R;
import com.lc.reggie.entity.ShoppingCart;
import com.lc.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController
{
	@Autowired
	private ShoppingCartService shoppingCartService;
	
	/**
	 * 添加到购物车
	 * @param shoppingCart
	 * @return
	 */
	@PostMapping("/add")
	public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart)
	{
		log.info("购物车数据:{}",shoppingCart);
		
		//设置用户id，指定当前是哪个用户的购物车数据
		Long currentId = BaseContext.getCurrentId();
		shoppingCart.setUserId(currentId);
		
		
		LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShoppingCart::getUserId, currentId);
		
		Long dishId = shoppingCart.getDishId();
		if (dishId != null)
		{
			//添加到购物车的是菜品
			lqw.eq(ShoppingCart::getDishId, dishId);
		}
		else
		{
			//添加到购物车的是套餐
			lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
		}
		
		//查询当前菜品或者套餐是否在购物车中
		ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(lqw);
		if (shoppingCartServiceOne != null)
		{
			//如果已经存在，就在原来数量基础上加一
			shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber() + 1);
			shoppingCartService.updateById(shoppingCartServiceOne);
		}
		else
		{
			//如果不存在，则添加到购物车，数量默认就是一
			shoppingCart.setNumber(1);
			shoppingCart.setCreateTime(LocalDateTime.now());
			shoppingCartService.save(shoppingCart);
			shoppingCartServiceOne = shoppingCart;
		}
		
		return R.success(shoppingCartServiceOne);
	}
	
	/**
	 * 减少购物车数量
	 * @param shoppingCart
	 * @return
	 */
	@PostMapping("/sub")
	public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart)
	{
		Long currentId = BaseContext.getCurrentId();
		LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShoppingCart::getUserId, currentId);
		Long dishId = shoppingCart.getDishId();
		if (dishId != null)
		{
			lqw.eq(ShoppingCart::getDishId, dishId);
		}
		else
		{
			lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
		}
		ShoppingCart one = shoppingCartService.getOne(lqw);
		//判断是否数量为1
		if (one.getNumber() == 1)
		{
			//直接删除好吧
			shoppingCartService.removeById(one.getId());
		}
		else
		{
			//数量减一
			one.setNumber(one.getNumber() - 1);
			shoppingCartService.updateById(one);
		}
		
		return R.success(one);
	}
	
	/**
	 * 查看购物车
	 * @return
	 */
	@GetMapping("/list")
	public R<List<ShoppingCart>> getList()
	{
		LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
		lqw.orderByDesc(ShoppingCart::getCreateTime);
		List<ShoppingCart> list = shoppingCartService.list(lqw);
		return R.success(list);
	}
	
	/**
	 * 清空购物车
	 * @return
	 */
	@DeleteMapping("/clean")
	public R<String> clean()
	{
		LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
		lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
		shoppingCartService.remove(lqw);
		return R.success("清空购物车成功");
	}
	
}
